﻿<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8" />
    <title>组织管理</title>
    <link href="../lib/layui/css/layui.min.css" rel="stylesheet" />
    <script type="text/javascript" src="../lib/Base64/base64.min.js"></script>
    <script type="text/javascript" src="../linq.js/linq.js"></script>
    <script src="../lib/layui/layui.js"></script>
</head>
<body>
    <div class="layui-fluid">
        <form class="layui-form" lay-filter="personForm">
            <input type="hidden" id="rid" name="rid" />
            <input type="hidden" id="oid" name="oid"/>
            <div class="layui-form-item layui-form-text" style="min-height:450px;">
                <div class="layui-input-inline">
                    <div class="person" id="person" lay-filter="person"></div>
                </div>
            </div>
            <div class="layui-form-item" style="text-align:center;">
                <a class="layui-btn" href="javascript:;" lay-submit lay-filter="saveperson">保存</a>
            </div>
        </form>

    </div>
    <script type="text/javascript">
        function getrequest() {
            var url = location.search; //获取url中"?"符后的字串
            var theRequest = new Object();
            if (url.indexOf("?") != -1) {
                var str = url.substr(1);
                strs = str.split("&");
                for (var i = 0; i < strs.length; i++) {
                    theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
                }
            }
            return theRequest;
        };

        layui.use(['transfer','form'], function () {
            var transfer = layui.transfer;
            var form = layui.form;
            var $ = layui.$;
            var item = JSON.parse(window.decodeURIComponent(atob(getrequest().iteminfo)));
            var personData = [];
            var transferValue = [];
            $(function () {
                $("#rid").val(item.rid);
                $("#oid").val(item.id);

            })
            $.ajax({
                type: 'get',
                url: localStorage.getItem('hosttingaddr') + '/api/organization/getpersons?did=' + item.did + '&oid=' + item.id,
                async: false,
                success: function (re) {
                    if (re.data != null && re.data.length > 0) {
                        personData = re.data;
                    }
                    if (re.checkedIds != null && re.checkedIds.length>0) {
                        transferValue = re.checkedIds;
                    }
                }
            });
            transfer.render({
                elem: '#person',//绑定元素
                id: 'person', //定义索引
                data: personData,
                title: ['未选择中', '选中'],
                value: transferValue,
                parseData: function (res) {
                    return {
                        "value": res.id,//数据值
                        "title": res.name,//数据标题
                        "disabled": res.disabled, //是否禁用
                        "checked": res.checked //是否选中
                    }
                }
            });
            //监听提交
            form.on('submit(saveperson)', function (data) {
                var data = data;
                var persons = transfer.getData('person');
                var pidArry = new Array();
                if (persons != null && persons.length>0) {
                    for (var i = 0; i < persons.length; i++) {
                        pidArry.push(persons[i].value)
                    }
                }
                data.field.did = item.did;
                data.field.Pids = pidArry;
                layui.$.ajax({
                    type: 'post',
                    url: localStorage.getItem('hosttingaddr') + '/api/organization/saveorganizationperson',
                    data: data.field,
                    dataType: 'json',
                    contentType: 'application/json;charset=utf-8',
                    data: JSON.stringify(data.field),
                    success: function (re) {
                        if (re.code == 0) {
                            layer.msg('保存成功!', { icon: 1, time: 1000 });
                            transfer.reload('person', {
                                value: re.data
                            })
                            var index = parent.layer.getFrameIndex(window.name); //获取当前窗口的name
                            parent.layer.close(index);
                        }
                    },
                    error: function (data) {
                        layer.msg('保存失败!', { icon: 1, time: 1000 });
                        var index = parent.layer.getFrameIndex(window.name); //获取当前窗口的name
                        parent.layer.close(index);
                    }
                });
                return false;
            });

        })
    </script>
</body>
</html>